#delimit;

clear;
clear matrix;
set memory 900m;

set more off;
cap log close; 

#delimit;

global MY_OUT_PATH  "[path where your master data set is stored]"; 
global MY_TEMP_PATH "[path where your temporary files are stored]";
global MY_FIGURE_PATH "[path where your figures and corresponding data sets are stored]";

#delimit; 
use "${MY_OUT_PATH}NEW_CNEF3_LSsample.dta", clear;

************************************************;
* define weights for survey;
************************************************;
#delimit;

svyset [pweight=w11102];

***********************************************
* check labor force status if working==0;
* lfs is current, while earnings are previous year - Change lfs to previous year;
***********************************************
#delimit;
preserve;
keep persnr year lfp;
replace year=year+1;
rename lfp lfp_new;
save "${MY_TEMP_PATH}changed_lfp", replace;
restore;

merge 1:1 persnr year using "${MY_TEMP_PATH}changed_lfp";
drop _merge;
drop lfp;

tab lfp_new if working==0;

**********************************************;
* sample selection;
*********************************************;
#delimit;

*keep households in the sample where at least one member is either working or unemployed (at least one member in labor forece)
#delimit;
egen lf=rowtotal(lfp_new working);
bys yearhhnr: egen lfp_hh=total(lf);
keep if lfp_hh>0 & lfp_hh!=.;

* keep only the observation of head (i.e. 1 observation per household);
#delimit;
keep if head==1;

***********************************************

#delimit;
preserve;
gen HHearningOECDgini=.;
gen gini_year=.;

#delimit cr
ineqdec0 HHearningOECD [aweight=w11102], by(year) 
local n = 1
forvalues i = 1984/2013 {
replace HHearningOECDgini= r(gini_`i') in `n'
replace gini_year= `i' in `n'
local ++n
}

keep  HHearningOECDgini gini_year
rename gini_year year
drop if year==.
save "${MY_FIGURE_PATH}HHearningOECDgini.dta", replace
restore




